package com.dqv5.k8s.bookinfo.controller;

import com.dqv5.k8s.bookinfo.entity.BookInfo;
import com.dqv5.k8s.bookinfo.service.BookService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author duqian
 * @date 2020/9/25
 */
@RestController
public class BookController {
    @Resource
    private BookService bookService;

    @GetMapping("/queryStatic")
    public ResponseEntity<Object> queryStatic() {
        BookInfo book = new BookInfo();
        book.setAuthor("张三");
        book.setTitle("示例标题");
        book.setDescription("示例简介");
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", book);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/queryAll")
    public ResponseEntity<Object> queryAll() {
        List<BookInfo> list = bookService.queryAll();
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", list);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/detail/{id}")
    public ResponseEntity<Object> detail(@PathVariable Long id) {
        BookInfo bookInfo = bookService.queryById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", bookInfo);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/insert")
    public ResponseEntity<Object> insert(@RequestBody BookInfo bookInfo) {
        bookService.insert(bookInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update")
    public ResponseEntity<Object> update(@RequestBody BookInfo bookInfo) {
        bookService.update(bookInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        bookService.delete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }


}
